package com.hdaccp.news1.controller;

import com.hdaccp.news1.dao.NewsDao;
import com.hdaccp.news1.entity.News;
import com.hdaccp.news1.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsDao newsDao ;
    @Autowired
    private NewsService newsService ;


    @RequestMapping("/query")
    public List<News> query(){
        return newsDao.findAll() ;
    }

    @RequestMapping("/getNewsById/{id}")
    public News getNewsById(@PathVariable("id") int id){
        return newsDao.findById(id).get() ;
    }

    @RequestMapping("/save/{title}/{content}/{createTime}")
    public News save(@PathVariable("title") String title,@PathVariable("content")String content,@PathVariable("createTime")String createTime){
        News news = new News() ;
        news.setContent(content);
        news.setCreateTime(createTime) ;
        news.setTitle(title);
        news = newsDao.save(news) ;
        return  news ;
    }
    @RequestMapping("/queryTop/{top}")
    public List<News> queryTop(@PathVariable("top") int top){
        return newsService.queryTop(top) ;
    }

}
